package cn.exrick.xboot.modules.Item.web;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.entity.Role;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.service.*;
import cn.exrick.xboot.modules.base.service.mybatis.IUserRoleService;
import cn.exrick.xboot.modules.portalConfig.entity.PortalConfig;
import cn.exrick.xboot.modules.portalConfig.service.IPortalConfigService;
import cn.exrick.xboot.modules.portalConfig.serviceimpl.IPortalConfigServiceImpl;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/xboot/usertest")
public class UserControllertest {


    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private IUserRoleService iUserRoleService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private DepartmentHeaderService departmentHeaderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SecurityUtil securityUtil;

    @PersistenceContext
    private EntityManager entityManager;


    //本方法负责处理/user/login.do 这个url 并且是由post方法方法传递过来的请求
    //@RequestMapping(value = "login.do", method = RequestMethod.GET)
    // 自动序列化成json

    /*    public String login() {

            System.out.println("交互测试20190706");
            return "[{\"name\":\"jack\",\"age\":\"21\"},{\"name\":\"jack\",\"age\":\"21\"}]";
        }*/
    public Result<Object> login() {

        System.out.println("交互测试20190706");
        // return new ResultUtil<Object>().setSuccessMsg("[{\"name\":\"jack\",\"age\":\"21\"},{\"name\":\"jack\",\"age\":\"21\"}]");
        //return "[{\"name\":\"jack\",\"age\":\"21\"},{\"name\":\"jack\",\"age\":\"21\"}]";
        return new ResultUtil<Object>().setData("你的数据");
    }


    @RequestMapping(value = "/login.do", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取用户列表")
    public Result<Page<User>> getByCondition(@ModelAttribute User user,
                                             @ModelAttribute SearchVo searchVo,
                                             @ModelAttribute PageVo pageVo) {

        Page<User> page = userService.findByCondition(user, searchVo, PageUtil.initPage(pageVo));
        for (User u : page.getContent()) {
            // 关联部门
            if (StrUtil.isNotBlank(u.getDepartmentId())) {
                Department department = departmentService.get(u.getDepartmentId());
                u.setDepartmentTitle(department.getTitle());
            }
            // 关联角色
            List<Role> list = iUserRoleService.findByUserId(u.getId());
            u.setRoles(list);
            // 清除持久上下文环境 避免后面语句导致持久化
            entityManager.clear();
            u.setPassword(null);
        }
        return new ResultUtil<Page<User>>().setData(page);
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取用户列表")
    public void test(@Param("userCode") String userCode, HttpServletResponse response) {
        log.info("userCode测试："+userCode);
        try{
            response.sendRedirect("http://www.baidu.com");

        }catch (Exception e){
            e.printStackTrace();
        }

        //return new ResultUtil<Page<User>>().setData(page);
    }


}
